package com.example.demo.service;

import com.example.demo.entity.User;
import com.example.demo.entity.Log;
import com.example.demo.entity.MusicDaily;
import com.example.demo.mapper.UserMapper;
import com.example.demo.mapper.LogMapper;
import com.example.demo.mapper.MusicDailyMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public
class UserImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public User login(String user_name, String password) {
        User user = userMapper.findUser(user_name, password);
        return user; // 如果用户存在，返回用户对象，否则返回 null
    }

    @Override
    public Integer updateUserInfo(User user) {
        return userMapper.updateUserInfo(user);
    }

    @Override
    public User getUserinfo(Integer userId) {
        return userMapper.getUserById(userId);
    }

    //判断用户登录用户名和密码正确性
//    @Override
//    public boolean login(String user_name, String password) {
//        User user = userMapper.findUser(user_name, password);
//        return user!= null;
//    }

    @Autowired
    private LogMapper logMapper;

    @Autowired
    private MusicDailyMapper musicDailyMapper;

    @Override
    public List<User> findUserAll() {
        return userMapper.findUserAll();
    }

    @Override
    public User findUserID(Integer userId) {
        return userMapper.findUserID(userId);
    }

    @Override
    public Integer insertUser(User user) {
        return userMapper.insertUser(user);
    }
    // 统计用户总人数
    @Override
    public Integer getTotalUserCount() {
        return userMapper.getTotalUsers();
    }

    // 统计活跃用户人数
    @Override
    public Integer getActiveUserCount() {
        return userMapper.getActiveUsers();
    }

    // 统计新增用户人数
    @Override
    public Integer getNewUserCount() {
        return userMapper.getNewUsers();
    }

    // 统计封禁用户人数
    @Override
    public Integer getBannedUserCount() {
        return userMapper.getBannedUsers();
    }

    @Override
    public Integer updateUser(User user) {
        return userMapper.updateUser(user);
    }

    @Override
    public Integer deleteUser(Integer userId) {
        return userMapper.deleteUser(userId);
    }

    @Override
    public PageInfo<User> findUserByPage(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);  // 设置分页参数
        List<User> users = userMapper.findUserAll();  // 查询用户列表
        return new PageInfo<>(users);  // 返回分页结果
    }

    @Override
    public Map<String, Object> getCompleteUserDetails(Integer userId) {
        Map<String, Object> userDetails = new HashMap<>();

        // 获取用户基本信息
        User user = userMapper.findUserID(userId);
        userDetails.put("userInfo", user);

        // 获取用户的登录日志
        List<Log> loginLogs = logMapper.selectLogsByUserId(userId);
        userDetails.put("loginLog", loginLogs);

        // 获取用户的每日使用时长
        List<MusicDaily> dailyUsage = musicDailyMapper.selectDailyPlaybackByUserId(userId);
        userDetails.put("dailyUsage", dailyUsage);

        return userDetails;
    }

    //注册页新增用户
    @Override
    public Integer insertUserRegister(User user) {
        // 检查用户名是否已存在
        if (userMapper.existsByUsername(user.getUsername())) {
            throw new DuplicateKeyException("用户名已存在");
        }
        // 插入新用户
        return userMapper.insertUserRegister(user.getUsername(), user.getPassword(), user.getProfilePicture());
    }

    //注册页修改密码
    @Override
    public Integer updatePassword(String user_name, String newPassword) {
        return userMapper.updateUserPassword(user_name, newPassword);
    }
}
